//
//  ClassifyViewController.m
//  MoGuRadio
//
//  Created by 李响 on 15/6/12.
//  Copyright (c) 2015年 mogu.com 蘑菇科技. All rights reserved.
//

#import "ClassifyOneViewController.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "Table.h"
#import "TableViewOneCell.h"
#import "SpecialPageViewController.h"
#import "MJRefresh.h"
#import "qiangXingXiuGai.h"
@interface ClassifyOneViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *tempArr;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) NSMutableArray *sourceArr;
@property (nonatomic, retain) UIView *viewZDY;
@end

@implementation ClassifyOneViewController
- (void)dealloc
{
    [_tableView release];
    [_sourceArr release];
    [_tempArr release];
    [_array release];
    
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.sourceArr =[NSMutableArray array];
        self.tempArr = [NSMutableArray array];
        self.array =  [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.945 alpha:1.0];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height / 13)];
        view.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.945 alpha:1.0];
    view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    [self.view addSubview:view];
    
    [view release];
    
    [self analysis];
    [self setUpTableView];
    
}

//- (void)showMenu:(UIButton *)button
//{
//    ClassifyOneViewController *secondVC = [[ClassifyOneViewController alloc] init];
//    [self.navigationController pushViewController:secondVC animated:YES];
//    secondVC.keBian2 = @"25";
//    [secondVC release];
//}

//- (void)loadView
//{
//    [super loadView];
//    CGRect frame = [[UIScreen mainScreen] applicationFrame];
//    self.view = [[UIView alloc] initWithFrame:frame];
//    self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
//
//    self.buttonTwo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//    self.buttonTwo.frame = CGRectMake(0, 64, self.view.bounds.size.width / 5, self.view.bounds.size.height / 16);
//    [self.buttonTwo setTitle:@"全部" forState:UIControlStateNormal];
//    [self.buttonTwo addTarget:self action:@selector(showMenu:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:_buttonTwo];
//}

//- (void)showMenu:(UIButton *)sender
//{
//    NSArray *menuItems =
//    @[
//
//      [KxMenuItem menuItem:@"全部"
//                     image:nil
//                    target:nil
//                    action:NULL],
//
//      [KxMenuItem menuItem:@"类型FM"
//                     image:[UIImage imageNamed:@"520"]
//                    target:self
//                    action:@selector(pushMenuItem:)],
//
//      [KxMenuItem menuItem:@"艺人FM"
//                     image:[UIImage imageNamed:@"520"]
//                    target:self
//                    action:@selector(pushMenuItem:)],
//
//      [KxMenuItem menuItem:@"场景FM"
//                     image:[UIImage imageNamed:@"520"]
//                    target:self
//                    action:@selector(pushMenuItem:)],
//
//      [KxMenuItem menuItem:@"流派FM"
//                     image:[UIImage imageNamed
//                            :@"520"]
//                    target:self
//                    action:@selector(pushMenuItem:)],
//
//      [KxMenuItem menuItem:@"年代FM"
//                     image:[UIImage imageNamed:@"520"]
//                    target:self
//                    action:@selector(pushMenuItem:)],
//      ];
//
//    KxMenuItem *first = menuItems[0];
//    first.foreColor = [UIColor colorWithRed:47/255.0f green:112/255.0f blue:225/255.0f alpha:1.0];
//
//    first.alignment = NSTextAlignmentCenter;
//
//    [KxMenu showMenuInView:self.view
//                  fromRect:sender.frame
//                 menuItems:menuItems];
//}
//
//- (void) pushMenuItem:(id)sender
//{
//    NSLog(@"%@", sender);
//}

- (void)analysis
{
    NSString *str = @"http://api.kaolafm.com/api/v3.1/typeradio/list?ids=-1&installid=0000WJRD&udid=b7c459c16cb33d45c20bb8fa0be1bb29&sessionid=b7c459c16cb33d45c20bb8fa0be1bb291434416069203&imsi=460036090834220&operator=3&network=1×tamp=1434418275&playid=d081b2d0b1f2849f9ad85123232c15af&sign=f383a93642cd5aa9f1828359a47f2db3&appid=0&channel=A-baidu&version=3.5.3&devicetype=0&resolution=480*854&";
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manger GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSDictionary *dic = [responseObject objectForKey:@"result"];
        self.tempArr = [dic objectForKey:@"dataList"];
        
        [self.tableView headerEndRefreshing];
        
        [self shuZu1];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接错误" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
        [alert show];
    }];
}

- (void)shuZu1
{
    for (NSDictionary *tempDic in self.tempArr) {
        NSArray *array1 = [tempDic objectForKey:@"dataList"];
        [self.array addObjectsFromArray:array1];
    }
    self.sourceArr = [Table mainArrWithDicArr:self.array];
    [self.tableView reloadData];
}
- (void)setUpTableView
{
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height / 13 + 64, self.view.bounds.size.width, self.view.bounds.size.height - (self.view.bounds.size.height / 13 + 64 + 49))style:UITableViewStylePlain];
    //self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView addHeaderWithTarget:self action:@selector(analysis)];
    [self.tableView headerBeginRefreshing];
    
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[TableViewOneCell class] forCellReuseIdentifier:@"diyi"];
    [_tableView release];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TableViewOneCell *cell = [tableView dequeueReusableCellWithIdentifier:@"diyi"];
    Table *main = [self.sourceArr  objectAtIndex:indexPath.row];
    cell.main = main;
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.sourceArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.view.bounds.size.height / 8;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    Table *teble = [self.sourceArr objectAtIndex:indexPath.row];
    qiangXingXiuGai *specialVC = [[qiangXingXiuGai alloc] init];
    specialVC.rid = [[[self.array objectAtIndex:indexPath.item] objectForKey:@"rid"] description];
    specialVC.pic = [[self.array objectAtIndex:indexPath.item] objectForKey:@"pic"];
    specialVC.hostName = @"暂无";
    specialVC.foucsNum = [[self.array objectAtIndex:indexPath.item] objectForKey:@"followedNum"];
    specialVC.listenNum = [[self.array objectAtIndex:indexPath.item] objectForKey:@"listenNum"];
    specialVC.descRadio = [[self.array objectAtIndex:indexPath.item] objectForKey:@"radioDesc"];
    specialVC.titleLabel = [[self.array objectAtIndex:indexPath.item] objectForKey:@"rname"];
    [self.navigationController pushViewController:specialVC animated:YES];
    [specialVC release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
